<?php

namespace App\Http\Controllers;

require_once app_path('Libs') . '/../../vendor/alipay_app/wappay/service/AlipayTradeService.php';
require_once app_path('Libs') . '/../../vendor/alipay_app/wappay/buildermodel/AlipayTradeWapPayContentBuilder.php';
use AlipayTradeWapPayContentBuilder;
use AlipayTradeService;

class AliPayController extends Controller
{
    public function ali_pay($data, $scene = 'job')
    {
        $payRequestBuilder = new AlipayTradeWapPayContentBuilder();
        $payRequestBuilder->setBody($data['body']);
        $payRequestBuilder->setSubject($data['subject']);
        $payRequestBuilder->setOutTradeNo($data['out_trade_no']);
        $payRequestBuilder->setTotalAmount($data['total_amount']);
        $payRequestBuilder->setTimeExpress($data['timeout_express']);

        $config = config('pay.ali_pay');
        $payResponse = new AlipayTradeService($config);

        if ($scene == 'job') {
            $notifyUrl = $config['notify_url_job'];
            $returnUrl = $config['return_url_job'];
        } elseif ($scene == 'shop') {
            $notifyUrl = $config['notify_url_shop'];
            $returnUrl = $config['return_url_shop'];
        }

        $result = $payResponse->wapPay($payRequestBuilder, $returnUrl, $notifyUrl);

        return $result;
    }
}
